package com.huaxin.hxmoduleworkorder.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huaxin.hxmoduleworkorder.common.enums.MaintenanceOrderOperation;
import com.huaxin.hxmoduleworkorder.common.enums.MaintenanceOrderOperatorRole;
import com.huaxin.hxmoduleworkorder.common.enums.OrderType;
import com.huaxin.hxmoduleworkorder.dto.ResponseCode;
import com.huaxin.hxmoduleworkorder.dto.ResponseResult;
import com.huaxin.hxmoduleworkorder.dto.request.CustomerEvaluationCreateReqDTO;
import com.huaxin.hxmoduleworkorder.dto.request.CustomerEvaluationUpdateReqDTO;
import com.huaxin.hxmoduleworkorder.dto.request.GetCustomerEvaluationPageReqDTO;
import com.huaxin.hxmoduleworkorder.dto.request.ReplyCustomerEvaluationReqDTO;
import com.huaxin.hxmoduleworkorder.models.CustomerEvaluation;
import com.huaxin.hxmoduleworkorder.service.CustomerEvaluationService;
import com.huaxin.hxmoduleworkorder.service.MaintenanceOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "客户评价")
@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/workorder/customerEvaluation")
public class CustomerEvaluationController {
    private final CustomerEvaluationService customerEvaluationService;
    private final MaintenanceOrderService maintenanceOrderService;

    /**
     * 新增客户评价
     *
     * @param dto 客户评价请求参数
     * @return 是否保存成功
     */
    @Operation(summary = "新增客户评价", description = "新增客户评价")
    @PostMapping("/addCustomerEvaluation")
    public ResponseResult<String> addCustomerEvaluation(@RequestBody @Valid CustomerEvaluationCreateReqDTO dto) {
        switch (OrderType.of(dto.getOrderType())) {
            case INSPECTION:
                break;
            case MAINTENANCE:
                if (!maintenanceOrderService.canOperate(dto.getMaintenanceOrderId(), MaintenanceOrderOperation.EVALUATE, MaintenanceOrderOperatorRole.of(dto.getEvaluatorRole()))) {
                    return ResponseResult.failure(ResponseCode.FORBIDDEN, "当前状态不能评价或无权限评价工单", null);
                }
                break;
            case INSTALLATION:
            case OTHER:
                break;
            default:
                return ResponseResult.failure(ResponseCode.BAD_REQUEST, "工单类型错误", null);
        }
        return customerEvaluationService.addCustomerEvaluation(dto);
    }

    /**
     * 更新客户评价（通过ID）
     *
     * @param dto 客户评价更新参数，必须包含ID
     * @return 是否更新成功
     */
    @Operation(summary = "更新客户评价", description = "更新客户评价")
    @PostMapping("/updateCustomerEvaluation")
    public ResponseResult<String> updateCustomerEvaluation(@RequestBody CustomerEvaluationUpdateReqDTO dto) {
        return customerEvaluationService.updateCustomerEvaluation(dto);
    }

    /**
     * 删除客户评价（通过ID）
     *
     * @param id 客户评价ID
     * @return 是否删除成功
     */
    @Operation(summary = "删除客户评价", description = "删除客户评价")
    @DeleteMapping("/deleteCustomerEvaluation")
    public ResponseResult<String> deleteCustomerEvaluation(@Parameter(description = "ID") @RequestParam(name = "id", required = true) String id) {
        return customerEvaluationService.deleteCustomerEvaluation(id);
    }

    /**
     * 根据ID获取客户评价详情
     *
     * @param id 客户评价ID
     * @return 客户评价对象，若无则返回 null
     */

    @Operation(summary = "根据ID获取客户评价详情", description = "根据ID获取客户评价详情")
    @GetMapping("/getCustomerEvaluationById")
    public ResponseResult<CustomerEvaluation> getCustomerEvaluationById(@Parameter(description = "ID") @RequestParam(name = "id", required = true) String id) {
        return customerEvaluationService.getCustomerEvaluationById(id);
    }

    /**
     * 根据工单ID获取评价信息
     *
     * @param maintenanceOrderId 工单ID
     * @return 评价信息，若无则返回 null
     */
    @Operation(summary = "根据工单ID获取评价信息", description = "根据工单ID获取评价信息")
    @GetMapping("/getEvaluationByOrderId")
    public ResponseResult<CustomerEvaluation> getEvaluationByOrderId(@Parameter(description = "工单ID") @RequestParam(name = "maintenanceOrderId", required = true) String maintenanceOrderId) {
        return customerEvaluationService.getEvaluationByOrderId(maintenanceOrderId);
    }

    /**
     * 判断客户是否已评价
     *
     * @param maintenanceOrderId 工单ID
     * @param customerId         客户ID
     * @return true 表示已评价，false 表示未评价
     */
    @Operation(summary = "判断客户是否已评价", description = "判断客户是否已评价")
    @GetMapping("/hasEvaluated")
    public ResponseResult<Boolean> hasEvaluated(@Parameter(description = "工单ID") @RequestParam(name = "maintenanceOrderId", required = true) String maintenanceOrderId,
                                                @Parameter(description = "客户ID") @RequestParam(name = "customerId", required = true) String customerId) {
        return ResponseResult.ok(customerEvaluationService.hasEvaluated(maintenanceOrderId, customerId));
    }

    /**
     * 获取客户评价列表（分页）
     *
     * @param in 分页参数
     * @return 客户评价列表
     */
    @Operation(summary = "获取客户评价列表", description = "获取客户评价列表")
    @PostMapping("/getCustomerEvaluationPage")
    public ResponseResult<Page<CustomerEvaluation>> getCustomerEvaluationPage(@RequestBody GetCustomerEvaluationPageReqDTO in) {
        return customerEvaluationService.getCustomerEvaluationPage(in);
    }

    /**
     * 回复客户评价
     *
     * @param in 回复参数
     * @return 接口返回的结果
     */
    @Operation(summary = "回复客户评价", description = "回复客户评价")
    @PostMapping("/replyCustomerEvaluation")
    public ResponseResult<String> replyCustomerEvaluation(@RequestBody ReplyCustomerEvaluationReqDTO in) {
        return customerEvaluationService.replyCustomerEvaluation(in.getId(), in.getReplyContent());
    }
}
